/*
 * Alloy Analyzer
 * Copyright (c) 2002 Massachusetts Institute of Technology
 *
 * The Alloy Analyzer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * The Alloy Analyzer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General License for more details.
 *
 * You should have received a copy of the GNU General License
 * along with the Alloy Analyzer; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package alloy.symm;

import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import alloy.type.BasicType;
import alloy.util.ObjID;

/**
 * Represents an immutable set of atoms of some {@link BasicType}.
 * Used by {@link AvailableSymmetry} to represent a set
 * of interchangeable atoms.
 * The {@link #equals} method tests object equality, not
 * structural equality like {@link Set#equals}.
 */
class AtomSet extends ObjID {
    private SortedSet /* of Integer */ _atoms = new TreeSet();
    private int[] _atomInds;

    /** Construct an {@link AtomSet} containing the given range of atoms */
    AtomSet(int fromAtom_, int toAtom_) {
	for (int i=fromAtom_; i<toAtom_; i++)
	    _atoms.add(new Integer(i));
	_atoms = Collections.unmodifiableSortedSet(_atoms);
    }

    /** Return the immutable, {@link SortedSet} of {@link Integer} atoms */
    SortedSet /* of Integer */ atoms() { return _atoms; }

    /** Test whether a given atom is in the set */
    boolean contains(int atom_) { return _atoms.contains(new Integer(atom_)); }
    
    /** Return the size of the set */
    int size() { return _atoms.size(); }

    /** Return the i'th atom in the set */
    int getAtomAt(int i_) {
	int[] atomInds = getAtomInds();
	return atomInds[i_];
    }


    /** Return an int[] with indices of the atoms in the set, in increasing order */
    int[] getAtomInds() {
	if (_atomInds  == null) {
	    _atomInds = new int[size()];
	    int i=0;
	    for (Iterator atomItr = _atoms.iterator(); atomItr.hasNext();) {
		Integer atom = (Integer)atomItr.next();
		_atomInds[i++] = atom.intValue();
	    }
	}
	return _atomInds;
    }

    public String toString() {
	return _atoms.toString();
    }
}  // class AtomSet
